﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RooHui.Infrastructure.Entities;
using RooHui.Portal.Biz.Model;
using RooHui.Portal.Biz;
using RooHui.Tool;
using RooHui.Application.CommonWebLogic;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.Portal.Web.Controllers
{
    public class ProjectController : Controller
    {
        public ActionResult List(int page = 0)
        {
            ViewBag.Projects = ProjectBiz.GetProjectList(page, 10);
            return View();
        }
        public ActionResult Detail(int id)
        {
            var project = ProjectBiz.GetProjectDetail(id);
            if (project == null)
            {
                return RedirectToAction("NoProject");
            }
            else
            {
                ViewBag.ProjectDetail = project;
                ViewBag.PromotionPolicies = ProjectBiz.GetAllEnablePromotionPolicies();
            }
            return View();
        }
        public ActionResult NoProject()
        {
            return View();
        }
        [HttpGet]
        public string ProjectSchedule(string pids)
        {
            List<ProjectInvestmentScheduleModel> list = ProjectBiz.GetProjectInvestmentSchedule(pids.Split(','));
            return list.ToJson();
        }

        [Auth(MemberType.Invester)]
        public ActionResult ProjectInvestment(int id)
        {
            var project = ProjectBiz.GetProjectDetail(id);
            if (project.ProjectStatus != ProjectStatus.Publish || project.ReleaseTime > DateTime.Now)
            {
                return RedirectToAction("Detail", new { id = id });
            }
            var account = MemberBiz.GetAccount(HttpContext.User.Identity.Name);
            
            if (project == null)
            {
                return RedirectToAction("NoProject");
            }
            else
            {
                ViewBag.Account = account;
                ViewBag.ProjectDetail = project;
                var list = ProjectBiz.RepaymentCalculate(project.ProjectId, project.IncreaseMoney);
                ViewBag.Plan = list.GroupBy(c => c.PayBackDate).OrderBy(c=>c.Key).ToList();

                var check = SupportBiz.GetCheckCode(HttpContext.User.Identity.Name, CheckCodeType.InvestmentMemberCheckCode, 24);
                ViewBag.Token = Tool.Cryptogram.CommonEncrypt(HttpContext.User.Identity.Name, check.Code);

                ViewBag.MyRemainInvestAmount = ProjectBiz.GetProjectInvestmentRemain(project.ProjectId, HttpContext.User.Identity.Name);
                ViewBag.PromotionPolicies = ProjectBiz.GetAllEnablePromotionPolicies();
            }
            return View();
        }
        [Auth(MemberType.Invester)]
        public string getInvestPlan(int projectId, string invest)
        {
            decimal iv = 0;
            decimal.TryParse(invest, out iv);
            var list = ProjectBiz.RepaymentCalculate(projectId, iv);
            if (list != null)
            {
                var data = list.GroupBy(c => c.PayBackDate).OrderBy(c=>c.Key).ToList();
                return data.ToJson();
            }
            return "";
        }
        [Auth(MemberType.Invester)]
        [HttpPost]
        public ActionResult ProjectInvestment(int projectId, string token, string invest)
        {
            var code = SupportBiz.GetCheckCode(HttpContext.User.Identity.Name, CheckCodeType.InvestmentMemberCheckCode,24, false);
            if (Tool.Cryptogram.CommonDecrypt(token, code.Code) != HttpContext.User.Identity.Name)
            {
                return RedirectToAction("ProjectInvestment", new { id=projectId });
            }
            decimal iv = 0;
            if (!Decimal.TryParse(invest, out iv))
            {
                return RedirectToAction("InvestmentResult", new { projectId = projectId });
            }
            if (!new Project().Any(c=>c.ID == projectId))
            {
                return RedirectToAction("NoProject");
            }
            int result = ProjectBiz.DoInvestment(projectId, HttpContext.User.Identity.Name, iv);
            if (result == 9)
            {
                return RedirectToAction("InvestmentResult", new { projectId = projectId });
            }
            else 
            {
                ViewBag.Invest=invest;
                switch(result){
                    default:
                    case 0:
                        ViewBag.Message = "项目基础信息错误，请重试。";
                        break;
                    case -1:
                        ViewBag.Message = "对不起，此项目已经满标。";
                        break;
                    case -2:
                        ViewBag.Message = "对不起，您的账户余额不足。请先充值！";
                        break;
                    case -3:
                        ViewBag.Message = "投资额必须为单份金额的整数倍";
                        break;
                    case -4:
                        ViewBag.Message = "个人投资超出最大限额";
                        break;
                    case -5:
                        ViewBag.Message = "投资额超过项目剩余额度";
                        break;
                    case -9:
                        ViewBag.Message = "投标失败，请稍后重试";
                        break;
                    case -10:
                        ViewBag.Message = "您的账号还未完成实名认证，请先完成实名";
                        break;
                    case -11:
                        ViewBag.Message = "您的账号还未完成激活，请先激活账户'<a href=\"/account/authcheck?u="+EncodeByBase64.Encode(HttpContext.User.Identity.Name)+"\">点击激活</a>'";
                        break;
                    case -12:
                        ViewBag.Message = "当前用户角色不能进行投资，请使用投资人账号";
                        break;
                    case -13:
                        ViewBag.Message = "您的当前账号被冻结，如有疑问请联系管理员";
                        break;
                }
            }

            var project = ProjectBiz.GetProjectDetail(projectId);
            var account = MemberBiz.GetAccount(HttpContext.User.Identity.Name);
            ViewBag.Account = account;
            ViewBag.ProjectDetail = project;
            var list = ProjectBiz.RepaymentCalculate(project.ProjectId, project.IncreaseMoney);
            ViewBag.Plan = list.GroupBy(c => c.PayBackDate).OrderBy(c => c.Key).ToList();
            var check = SupportBiz.GetCheckCode(HttpContext.User.Identity.Name, CheckCodeType.InvestmentMemberCheckCode, 24);
            ViewBag.Token = Tool.Cryptogram.CommonEncrypt(HttpContext.User.Identity.Name, check.Code);
            ViewBag.MyRemainInvestAmount = ProjectBiz.GetProjectInvestmentRemain(project.ProjectId, HttpContext.User.Identity.Name);
            ViewBag.PromotionPolicies = ProjectBiz.GetAllEnablePromotionPolicies();
            return View();
        }
        [Auth(MemberType.Invester)]
        public ActionResult InvestmentResult(int projectId)
        {
            var project = ProjectBiz.GetOrderDetailWithProject(projectId);
            var account = MemberBiz.GetAccount(HttpContext.User.Identity.Name);
            var investment = new Investment()
                .FindAll(c => c.UserName == HttpContext.User.Identity.Name && c.ProjectId == projectId && c.Status == (int)InvestmentStatus.Confrim)
                .OrderByDescending(c=>c.ID)
                .ToList();
            if (investment == null || !investment.Any())
            {
                return RedirectToAction("projectinvestment", new { id = projectId });
            }
            if (project == null)
            {
                return RedirectToAction("NoProject");
            }
            else
            {
                ViewBag.Account = account;
                ViewBag.ProjectDetail = project;
                ViewBag.Investment = investment;
                var list = ProjectBiz.InvestmentRepaymentCalculate(project.ProjectId, investment.Sum(c => c.InvestMentAmount));
                ViewBag.Plan = list.GroupBy(c => c.PayBackDate).OrderBy(c => c.Key).ToList();
            }
            return View();
        }
    }
}
